1 using System;
2 using
UnityEngine;
3
4 namespace
UnityStandardAssets.Vehicles.Aeroplane
5 {
6     
public class AeroplanePropellerAnimator : MonoBehaviour
7     {
8         [SerializeField]
private Transform m_PropellorModel; // The model of the the aeroplane's propellor.
9         [SerializeField]
private Transform m_PropellorBlur; // The plane used for the blurred propellor textures.
10         [SerializeField]
private Texture2D[] m_PropellorBlurTextures; // An array of increasingly blurred propellor textures.
11         [SerializeField] [Range(
0f, 1f)] private float m_ThrottleBlurStart = 0.25f; // The point at which the blurred textures start.
12         [SerializeField] [Range(
0f, 1f)] private float m_ThrottleBlurEnd = 0.5f; // The point at which the blurred textures stop changing.
13         [SerializeField]
private float m_MaxRpm = 2000; // The maximum speed the propellor can turn at.
14
15         
private AeroplaneController m_Plane; // Reference to the aeroplane controller.
16         
private int m_PropellorBlurState = -1; // To store the state of the blurred textures.
17         
private const float k_RpmToDps = 60f; // For converting from revs per minute to degrees per second.
18         
private Renderer m_PropellorModelRenderer;
19         
private Renderer m_PropellorBlurRenderer;
20
21
22         
private void Awake()
23         {
24             
// Set up the reference to the aeroplane controller.
25             m_Plane = GetComponent<AeroplaneController>();
26
27             m_PropellorModelRenderer = m_PropellorModel.GetComponent<Renderer>();
28             m_PropellorBlurRenderer = m_PropellorBlur.GetComponent<Renderer>();
29
30             
// Set the propellor blur gameobject's parent to be the propellor.
31             m_PropellorBlur.parent = m_PropellorModel;
32         }
33
34
35         
private void Update()
36         {
37             
// Rotate the propellor model at a rate proportional to the throttle.
38             m_PropellorModel.Rotate(
0, m_MaxRpm*m_Plane.Throttle*Time.deltaTime*k_RpmToDps, 0);
39
40             
// Create an integer for the new state of the blur textures.
41             
var newBlurState = 0;
42
43             
// choose between the blurred textures, if the throttle is high enough
44             
if (m_Plane.Throttle > m_ThrottleBlurStart)
45             {
46                 
var throttleBlurProportion = Mathf.InverseLerp(m_ThrottleBlurStart, m_ThrottleBlurEnd, m_Plane.Throttle);
47                 newBlurState = Mathf.FloorToInt(throttleBlurProportion*(m_PropellorBlurTextures.Length -
1));
48             }
49
50             
// If the blur state has changed
51             
if (newBlurState != m_PropellorBlurState)
52             {
53                 m_PropellorBlurState = newBlurState;
54
55                 
if (m_PropellorBlurState == 0)
56                 {
57                     
// switch to using the 'real' propellor model
58                     m_PropellorModelRenderer.enabled =
true;
59                     m_PropellorBlurRenderer.enabled =
false;
60                 }
61                 
else
62                 {
63                     
// Otherwise turn off the propellor model and turn on the blur.
64                     m_PropellorModelRenderer.enabled =
false;
65                     m_PropellorBlurRenderer.enabled =
true;
66
67                     
// set the appropriate texture from the blur array
68                     m_PropellorBlurRenderer.material.mainTexture = m_PropellorBlurTextures[m_PropellorBlurState];
69                 }
70             }
71         }
72     }
73 }


Gõ tìm kiếm nhanh...